package ntut.andengine.game;

import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

public class GameAbout extends GameScene {

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 800;
	
	private TextureRegion mProgrammerTextureRegion;
	private TextureRegion mAboutTextureRegion;
	private TextureRegion mBackTextureRegion;
	private TextureRegion mSpotLightTextureRegion;

	@Override
	public void onLoadResources() {
		super.onLoadResources();
		
		BitmapTexture bitmapTexture = new BitmapTexture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mProgrammerTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "programmer.png", 0, 0);
		mAboutTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "about.png", 0, 185);
		mBackTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "back.png", 345, 0);
		mSpotLightTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "spot_light.png", 345, 185);
		mEngine.getTextureManager().loadTexture(bitmapTexture);
	}

	@Override
	public Scene onLoadScene() {
		final int centerX = (CAMERA_WIDTH - this.mProgrammerTextureRegion.getWidth()) / 2;
		final int centerY = (CAMERA_HEIGHT - this.mProgrammerTextureRegion.getHeight()) / 2;
		
		final Sprite about = new Sprite(240 - this.mAboutTextureRegion.getWidth()/2, 100 - this.mAboutTextureRegion.getHeight()/2, this.mAboutTextureRegion);
		about.setScale(2);
		final Sprite programmer = new Sprite(centerX, centerY, mProgrammerTextureRegion);
		final Sprite back = new Sprite(40, 720, mBackTextureRegion){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameAbout.this, GameInit.class);
				}
				return true;
			}			
		};
		final Sprite light = new Sprite(240-40, 480-40-70, mSpotLightTextureRegion);
		light.setScale(6f);
		light.registerEntityModifier(new LoopEntityModifier(new RotationModifier(1, 0, 360)));
		
		
		
		mScene = new Scene();
		mScene.attachChild(about);
		mScene.attachChild(light);
		mScene.attachChild(programmer);
		mScene.attachChild(back);
		mScene.registerTouchArea(back);
		
		
//		/* UPDATE */	
//		mScene.registerUpdateHandler(new IUpdateHandler(){
//			float time;
//			
//			@Override
//			public void onUpdate(float pSecondsElapsed) {
//				time += pSecondsElapsed;
//				
//				if(time > 3){
//					//startActivity(GameAbout.this, GameInit.class);				
//				}
//			}
//
//			@Override
//			public void reset() {
//				// TODO Auto-generated method stub
//				
//			}
//			
//		});
		
		return mScene;
	}
}
